package com.l.web.master.login;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.Random;

@Component
@Scope("prototype")
public class CaptchaService {

    private int imageWidth = 70;
    private int imageHeight = 20;
    private int codeNumber = 4;
    private int backGround = 20;
    private String codes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    private Random random = new Random(new Date().getTime());

    public int getImageWidth() {
        return imageWidth;
    }

    public void setImageWidth(int imageWidth) {
        this.imageWidth = imageWidth;
    }

    public int getImageHeight() {
        return imageHeight;
    }

    public void setImageHeight(int imageHeight) {
        this.imageHeight = imageHeight;
    }

    public int getCodeNumber() {
        return codeNumber;
    }

    public void setCodeNumber(int codeNumber) {
        this.codeNumber = codeNumber;
    }

    public int getBackGround() {
        return backGround;
    }

    public void setBackGround(int backGround) {
        this.backGround = backGround;
    }

    private char getCode() {
        int index = random.nextInt(codes.length());
        return codes.charAt(index);
    }

    /**
     * 绘制干扰背景
     *
     * @param graphics 画笔
     */
    private void drawBackGround(Graphics graphics) {

        for (int i = 0; i < backGround; i++) {
            int red = random.nextInt(256);
            int green = random.nextInt(256);
            int blue = random.nextInt(256);
            graphics.setColor(new Color(red, green, blue));
            int startX = random.nextInt(imageWidth);
            int startY = random.nextInt(imageHeight);

            if (random.nextInt(5) % 3 == 0) {
                int r = random.nextInt(10);
                int startAngle = random.nextInt(360);
                int arcAngle = random.nextInt(360);
                graphics.drawArc(startX, startY, r, r, startAngle, arcAngle);
            } else {
                int changeX = random.nextInt() % 6;
                int changeY = random.nextInt() % 6;
                graphics.drawLine(startX, startY, startX + changeX, startY + changeY);
            }
        }
    }

    /**
     * 绘制随机内容
     *
     * @param graphics 画笔
     * @return 图片绘制的随机内容
     */
    private String drawContext(Graphics graphics) {
        StringBuffer validateCode = new StringBuffer();
        graphics.setFont(new Font("Tahoma", Font.BOLD, imageHeight - 3));

        for (int i = 0; i < codeNumber; i++) {
            int red = random.nextInt(100) + 50;
            int green = random.nextInt(100) + 50;
            int blue = random.nextInt(100) + 50;
            graphics.setColor(new Color(red, green, blue));
            char code = getCode();
            validateCode.append(code);
            graphics.drawString(code + "", imageWidth / codeNumber * i + 4, 16);
        }

        return validateCode.toString();
    }

    /**
     * 输出图片
     *
     * @param outputStream
     * @return 图片中的随机内容
     */
    public String draw(OutputStream outputStream) throws IOException {
        BufferedImage bufferedImage = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_BGR);
        Graphics2D g = bufferedImage.createGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, imageWidth, imageHeight);
        drawBackGround(g);
        String context = drawContext(g);

        try {
            ImageIO.write(bufferedImage, "jpeg", outputStream);
        } finally {
            bufferedImage.flush();
            outputStream.close();
        }

        return context;
    }
}
